//生成AddDayCount天之前/之后的年月日
export function GetDate(AddDayCount = 0) {
	var dd = new Date();
	dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期
	var y = dd.getFullYear();
	var m = dd.getMonth() + 1;//获取当前月份的日期
	var d = dd.getDate();
	if (m >= 1 && m <= 9) {
		m = "0" + m;
	}
	if (d >= 0 && d <= 9) {
		d = "0" + d;
	}
	return y + "-" + m + "-" + d;
}
//查询cha在str中第num个的位置,num从0开始
export function findStr(str, cha, num) {
	var x = str.indexOf(cha);
	for (var i = 0; i < num; i++) {
		x = str.indexOf(cha, x + 1);
	}
	return x;
}
//获取样式
export function getStyle(obj, attr) {
	if (obj.currentStyle) {
		//IE,OPER
		return obj.currentStyle[attr];
	} else {
		//chrome、safari、FireFox
		return window.getComputedStyle(obj)[attr];
	}
}
//判断是否含有某个类名
export function hasClass(elem, className) {
	return elem.className.indexOf(className) > -1;
}
//添加class
export function addClass(elem, className) {
	if (!this.hasClass(elem, className)) {
		if (!new RegExp("(^|\\s)" + className + "(\\s|$)").test(elem.className)) elem.className += ' ' + className;
	}
}
//移除class
export function removeClass(elem, className) {
	if (this.hasClass(elem, className)) {
		elem.className = elem.className.replace(new RegExp("(^|\\s)" + className + "(?=(\\s|$))", "g"), '');
	}
}
//获取IE版本
export function IEVersion() {
	var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
	var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器  
	var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
	var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
	if (isIE) {
		var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
		reIE.test(userAgent);
		var fIEVersion = parseFloat(RegExp["$1"]);
		if (fIEVersion == 7) {
			return 7;
		} else if (fIEVersion == 8) {
			return 8;
		} else if (fIEVersion == 9) {
			return 9;
		} else if (fIEVersion == 10) {
			return 10;
		} else {
			return 6;//IE版本<=7
		}
	} else if (isEdge) {
		return 'edge';//edge
	} else if (isIE11) {
		return 11; //IE11  
	} else {
		return -1;//不是ie浏览器
	}
}
export function clientXY(){
    if(window.innerHeight !== undefined){
        return {
            "width": window.innerWidth,
            "height": window.innerHeight
        }
    }else if(document.compatMode === "CSS1Compat"){
        return {
            "width": document.documentElement.clientWidth,
            "height": document.documentElement.clientHeight
        }
    }else{
        return {
            "width": document.body.clientWidth,
            "height": document.body.clientHeight
        }
    }
}

export function isEmptyObject(obj) {
	for (let key in obj) {
		return false
	};
	return true
}
export function isObject(obj){
    if(Object.prototype.toString.call(obj).toLowerCase() === '[object object]'){
        return true;
    }else{
        return false;
    }
}

export function addEvent(obj, type, callback, useCapture=false) {
    obj.addEventListener(type, callback, useCapture);
    addEventList(obj, type, callback, useCapture);
}

export function removeEvent (obj, type, callback, useCapture=false) {
    obj.removeEventListener(type, callback, useCapture);
    removeEventList(obj, type, callback, useCapture);
}

export function addEventList(obj, type, callback, useCapture=false) {
    if (obj.eventList) {
        if (obj.eventList[type]) {
            obj.eventList[type].push({ callback: callback, useCapture: useCapture });
        } else {
            obj.eventList[type] = [{ callback: callback, useCapture: useCapture }];
        }
    } else {
        obj.eventList = {};
        obj.eventList[type] = [{ callback: callback, useCapture: useCapture }];
    }
}

export function removeEventList(obj, type, callback, useCapture=false) {
	if (obj.eventList) {
		let eventList=obj.eventList;
        if (eventList[type]) {            
            for (let i = 0; i < eventList[type].length; i++) {
                if (eventList[type][i].callback===callback) {
                    eventList[type].splice(i, 1);
                    if (eventList[type].length===0) {
                        delete eventList[type];
                    }
                    break;
                }
            }
        } 
    } 
}

export function hasEvent(obj, type, callback, useCapture=false){
	let flag = false;
	if(obj.eventList){
		let eventList = obj.eventList;
        if(eventList[type]){
            for(let i=0;i<eventList[type].length; i++){
                if(eventList[type][i].callback === callback){
                    flag = true;
                }
            }
        }
    }
    return flag;
}